package org.balthie.demo.opensource.org.codehaus.plexus.compiler.util.scan;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.balthie.demo.opensource.org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;

public class SimpleSourceInclusionScanner extends AbstractSourceInclusionScanner
{
    public static void main(String[] args) throws Exception
    {
        Set<String> includes = null;
        if(includes == null || includes.isEmpty())
        {
            includes = Collections.singleton("%regex[.*groovy]");
        }
        Set<String> excludes = new HashSet<String>();
//    excludes.add("**/*");
        
        SimpleSourceInclusionScanner scanner = new SimpleSourceInclusionScanner(includes, excludes);
        scanner.addSourceMapping(new SuffixMapping(".groovy", ".class"));
        scanner.addSourceMapping(new SuffixMapping(".java", ".class"));
        
        File sourceDir = new File(
                "E:\\balthie-git\\viomi-gitlab\\viomi-java-architecture\\Modularity-Architecture-trunk\\component-batch\\src\\test\\java");
        File outputDir = new File(
                "E:\\balthie-git\\viomi-gitlab\\viomi-java-architecture\\Modularity-Architecture-trunk\\component-batch\\src\\mytest\\java");
        
        Set<File> files = scanner.getIncludedSources(sourceDir, outputDir);
        files.stream().forEach(System.out::println);
    }
    
    private final Set<String> sourceIncludes;
    
    private final Set<String> sourceExcludes;
    
    public SimpleSourceInclusionScanner(Set<String> sourceIncludes, Set<String> sourceExcludes)
    {
        this.sourceIncludes = sourceIncludes;
        
        this.sourceExcludes = sourceExcludes;
    }
    
    public Set<File> getIncludedSources(File sourceDir, File targetDir)
            throws Exception
    {
        List srcMappings = getSourceMappings();
        
        if(srcMappings.isEmpty())
        {
            return Collections.emptySet();
        }
        
        String[] potentialSources = scanForSources(sourceDir, this.sourceIncludes, this.sourceExcludes);
        
        Set matchingSources = new HashSet(potentialSources != null ? potentialSources.length : 0);
        
        if(potentialSources != null)
        {
            for(String potentialSource : potentialSources)
            {
                matchingSources.add(new File(sourceDir, potentialSource));
            }
        }
        
        return matchingSources;
    }
}